from django.conf import settings
from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.views.generic.simple import direct_to_template
import flickrapi

from location.models import Location, Photo
from map.forms import PhotoUploadForm

import sys

def location_detail(request, location_id):
    parentLoc = get_object_or_404(Location, pk=location_id)
    if parentLoc.type.internal_code == "mine":
        loc = parentLoc.mine
    if parentLoc.type.internal_code == 'quarry':
        loc = parentLoc.quarry
            
    return render_to_response('location/detail.html', {'location': loc}, context_instance=RequestContext(request))

def location_photos(request, location_id):
    loc = get_object_or_404(Location, pk=location_id)
    flickr_user_id = settings.FLICKR_USER_ID
    if loc.photos_count > 0:
        photos = loc.get_photos()

    return render_to_response('location/photosworklet.html', {'photos': photos, 'flickr_user_id': flickr_user_id}, context_instance=RequestContext(request))

def photo_upload(request):
    if request.method == 'POST':
        location_id = request.POST.get('location_id')
        location = get_object_or_404(Location, pk=location_id)
        photo = Photo(location=location)
        upload_form = PhotoUploadForm(request.POST, request.FILES, instance=photo)
        if upload_form.is_valid():
            newphoto = upload_form.save()
            
    return render_to_response('location/photouploadresult.html')
